package com.mzj.saas.mservice.sys.controller;

import com.mzj.saas.commons.ResultBean;
import com.mzj.saas.mservice.sys.service.LoginService;
import com.mzj.saas.mservice.sys.vo.AppUserVO;
import com.mzj.saas.mservice.sys.vo.PerUserVO;
import com.mzj.saas.mservice.sys.vo.UserCodeRequestVO;
import com.mzj.saas.mservice.sys.vo.WechatLoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 登录
 *
 * @author shun
 * @createDate: 2021年3月9日 上午11:32:56
 * @version: 1.0
 */
@Controller
@RequestMapping("/login")
@Api(tags = "登录接口", value = "小程序/企业登录")
public class LoginController {
    @Autowired
    private LoginService loginService;

    /**
     * 登录,实现用户登录账号和密码的校验
     *
     * @param vo   用户信息实体类
     * @param type 类型:0、普通用户，1、主管理员，2、子管理员
     *             1
     * @return
     * @author qixi
     * @data 2022/12/1
     */
    @PostMapping(value = "/authorize")
    @ResponseBody
    @ApiOperation("小程序/企业登录")
    public ResultBean<Map<String, Object>> authorizeUser(@RequestBody
                                                         @ApiParam(name = "vo", value = "用户登录信息") PerUserVO vo,
                                                         @RequestParam(name = "type", required = false)
                                                         @ApiParam(name = "type", value = "类型:0、普通用户，1、主管理员，2、子管理员") Integer type) {
        return loginService.login(vo);
    }

    /**
     * 登入测试
     *
     * @return
     */
    @RequestMapping(value = "/test")
    @ResponseBody
    public Object test(String mobile, String password) {
        PerUserVO vo = new PerUserVO();
        vo.setMobile(mobile);
        // vo.setPassword(password);
        Object object = loginService.login(vo);
        return ResultBean.successfulResult(object);
    }

    /**
     * 验证码登录/注册
     *
     * @return 登录信息
     * @author qixi
     * @date 2022/12/1
     */
    @PostMapping(value = "/appletCodeLogin")
    @ResponseBody
    @ApiOperation(notes = "用户验证码登录", value = "验证码登录/注册")
    public ResultBean<Object> codeLogin(@RequestBody UserCodeRequestVO userCodeRequestVO) {
        return loginService.codeLogin(userCodeRequestVO);
    }

    /**
     * 验证码登录/注册
     *
     * @return 登录信息
     * @author qixi
     * @date 2022/12/1
     */
    @PostMapping(value = "/codeLogin")
    @ResponseBody
    @ApiOperation(notes = "企业验证码登录", value = "验证码登录/注册")
    public ResultBean<Object> orgCodeLogin(@RequestBody UserCodeRequestVO userCodeRequestVO) {
        return loginService.orgCodeLogin(userCodeRequestVO);
    }


    /**
     * 微信授权登录(已授权过)
     *
     * @param openId
     * @return
     * @author shun
     * @date 2021年9月23日
     */
    @PostMapping(value = "/weiChatLogin")
    @ResponseBody
    public ResultBean<Object> weiChatLogin(@RequestParam(name = "openId", required = false) String openId) {
        return loginService.weiChatLogin(openId);
    }

    /**
     * 微信登录
     *
     * @param wechatLoginVo
     * @return
     */
    @PostMapping(value = "/weiChatBindingLogin")
    @ResponseBody
    @ApiOperation(notes = "微信登录", value = "微信登录一键登录")
    public ResultBean<Object> weiChatBindingLogin(@RequestBody WechatLoginVo wechatLoginVo) {
        return loginService.weiChatBindingLogin(wechatLoginVo);
    }

    /**
     * ios-app授权登录(已授权过)
     *
     * @return
     * @author shun
     * @date 2021年9月23日
     */
    @PostMapping(value = "/iosLogin")
    @ResponseBody
    public ResultBean<Object> iosLogin(@RequestParam(name = "iosOpenId", required = false) String iosOpenId) {
        return loginService.iosLogin(iosOpenId);
    }

    /**
     * ios-app绑定并登录(未授权过)
     *
     * @param appUserVO
     * @return
     * @author shun
     * @date 2021年9月23日
     */
    @PostMapping(value = "/iosBindingLogin")
    @ResponseBody
    public ResultBean<Object> iosBindingLogin(AppUserVO appUserVO) {
        return loginService.iosBindingLogin(appUserVO);
    }


    /**
     * 本机号码一键登录
     *
     * @return
     * @author xin
     * @date 2022年6月18日
     */
    @PostMapping(value = "/clickLogin_v1")
    @ResponseBody
    public ResultBean<Object> clickLogin_v1(@RequestBody AppUserVO vo) {
        return loginService.clickLogin(vo);
    }

    /**
     * 本机号码一键登录
     *
     * @return
     * @author xin
     * @date 2022年6月18日
     */
    @PostMapping(value = "/clickLogin")
    @ResponseBody
    public ResultBean<Object> clickLogin(@RequestBody AppUserVO vo) {
        return loginService.clickLoginV3(vo);
    }


    @GetMapping(value = "/gainOpenId")
    @ResponseBody
    public ResultBean<Object> gainOpenId(String code) {
        return loginService.gainOpenId(code);
    }

}
